package com.dolphin.common.expr.emus;

import com.dolphin.common.component.var.enums.ValueTypeEnum;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author shida
 */

@Getter
public enum OperateTypeEnum implements OperateTypeFunction{
    EQ("==","等于", "EQ", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.eq(realValue, needValues.get(0));
        }
    },
    NE("!=","不等于", "NE", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.eq(realValue, needValues.get(0)) == false;
        }
    },
    GT(">","大于", "GT", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.compareTo(realValue, needValues.get(0)) > 0;
        }
    },
    GQ(">=","大于等于", "GE", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.compareTo(realValue, needValues.get(0)) >= 0;
        }
    },
    LT("<","小于", "LT", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.compareTo(realValue, needValues.get(0)) < 0;
        }
    },
    LQ("<=","小于等于", "LE", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.compareTo(realValue, needValues.get(0)) <= 0;
        }
    },
    IN("in","在列表中", "IN", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.in(realValue, needValues);
        }
    },
    NOTIN("not in","不在列表中", "NOTIN", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.in(realValue, needValues) == false;
        }
    },
    BW("bw","之间", "BW", 2){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.compareTo(realValue, needValues.get(0)) >= 0
                    && valueType.compareTo(realValue, needValues.get(1)) <= 0;
        }
    },
    START_END("start end","以needValues[0]开头，以needValues[1]结尾", "START_END", 2){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return valueType.startAndEnd(realValue, needValues);
        }
    },
    LK("like","模糊匹配", "LK", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            for(String item : needValues){
                if(valueType.lk(realValue, item) == true){
                    return true;
                }
            }

            return false;
        }
    },
    NOTLIKE("not like","不匹配", "NOTLIKE", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            for(String item : needValues){
                if(valueType.lk(realValue, item) == true){
                    return false;
                }
            }

            return true;
        }
    },
    HAS_INTERSECTION("has intersection","有交集", "HAS_INTERSECTION", 1){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            List<String> intersection = valueType.intersection(realValue, needValues);
            return intersection != null && intersection.size() > 0;
        }
    },
    HASVAL("has value","有值", "HASVAL", 0){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            if(realValue == null){
                return false;
            }

            if(StringUtils.isEmpty(realValue.toString())){
                return false;
            }

            return true;
        }
    },
    NOHASVAL("no has value","无值", "NOHASVAL", 0){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            if(realValue == null){
                return true;
            }

            if(StringUtils.isEmpty(realValue.toString())){
                return true;
            }

            return false;
        }
    },
    NV("is null","为空", "NV", 0){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return realValue == null;
        }
    },
    NN("is not null","不为空", "NN", 0){
        @Override
        public boolean calculate(Object realValue, List<String> needValues, ValueTypeEnum valueType) {
            return realValue != null;
        }
    }
    ;

    private String code;
    private String description;
    private String type;
    private int valueSize;

    OperateTypeEnum(String code, String description, String type, int valueSize) {
        this.code = code;
        this.description = description;
        this.type = type;
        this.valueSize = valueSize;
    }

    public static OperateTypeEnum getByType(String type) {
        for (OperateTypeEnum operateTypeEnum : OperateTypeEnum.values()) {
            if (operateTypeEnum.getType().equals(type)) {
                return operateTypeEnum;
            }
        }
        return null;
    }
}
